/**
 * Created by yinxiaofei on 2018/3/27.
 */
$(".cartItem").click(function () {
    $(this).next().toggle(500);
})


$(".reduce").click(function () {
    var val = $(this).next().val();
    if(val != "" && val != 0){
        val = val-1;
        $(this).next().val(val);
        var circle = parseInt($(".circle").text())-1;
        var price = parseInt($(this).parents(".place-p").find(".price").text());
        var total = parseInt($("#total").text());
        $(".circle").text(circle);
        if(circle == 0){
            $(".circle").css("opacity","0");
        }
        total = total-price;

        $("#total").text(changeTwoDecimal_f(total));
    }
})

$(".plus").click(function () {
    var val = $(this).prev().val();
    if(val != ""){
        val = parseInt(val);
    }
    val = val+1;
    $(this).prev().val(val);
    var circle = parseInt($(".circle").text());
    var price = parseInt($(this).parents(".place-p").find(".price").text());
    var total = parseInt($("#total").text());
    $(".circle").css("opacity","1");
    $(".circle").text(circle+1);
    if (isNaN(total))
    {
        total = 0;
    }
    total = total+price;

    $("#total").text(changeTwoDecimal_f(total));
})

function changeTwoDecimal_f(x) {
    var f_x = parseFloat(x);
    if (isNaN(f_x)) {
        // alert('function:changeTwoDecimal->parameter error');
        return false;
    }
    var f_x = Math.round(x * 100) / 100;
    var s_x = f_x.toString();
    var pos_decimal = s_x.indexOf('.');
    if (pos_decimal < 0) {
        pos_decimal = s_x.length;
        s_x += '.';
    }
    while (s_x.length <= pos_decimal + 2) {
        s_x += '0';
    }
    return s_x;
}

if ( $("#birthday").length > 0 ) {
    $("#birthday").mobiscroll().date({
        theme: "android-ics",
        lang: "zh",
        display: 'bottom',

        // startYear: (new Date()).getFullYear(), //开始年份
        minDate: new Date(),
        endYear: (new Date()).getFullYear() + 9, //结束年份
        dateFormat: 'yy-mm-dd', //返回结果格式化为年月格式
        // wheels:[], 设置此属性可以只显示年月，此处演示，就用下面的onBeforeShow方法,另外也可以用treelist去实现
        //onBeforeShow: function (inst) { inst.settings.wheels[0].length>2?inst.settings.wheels[0].pop():null; }, //弹掉“日”滚轮
        headerText: function (valueText) { //自定义弹出框头部格式
            array = valueText.split('-');
            return array[0] + "年" + array[1] + "月" + array[2] + "日";
        },
        onSelect: function (valueText, inst) {
            $("#orderItem").val(valueText);
            /*$.post("inc/person.org.php", {apart: "resume_base", birthday: valueText}, function (result) {
             if (result == 'ok') {
             $(this).find(".mbase-menu-txt").html(valueText);
             }
             else {
             error('网络繁忙，请您稍后再试');
             }
             });*/ 这是后端入库部分
        }
    });
}
